package com.java2404.mp2.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java2404.mp2.entity.Student;
import com.java2404.mp2.mapper.StudentMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: 汤秦凯
 * @CreateTime: 2024/12/24
 * @Version: JDK 1.8
 */
@Api(tags = "学生管理")
@RequestMapping("student")
@RestController
public class StudentHandler {
    @Resource
    private StudentMapper studentMapper;

    //添加学生信息
    @ApiOperation("添加学生信息")
    @PostMapping("add")
    public String addStudent(@RequestBody @Valid Student student){
        int insert = studentMapper.insert(student);
        if (insert == 0){
            return "添加失败";
        }
        return "添加成功";
    }
    //删除学生信息
    @ApiOperation("删除学生信息")
    @DeleteMapping("delete/{id}")
    public String deleteStudent(@PathVariable Integer id){
        int delete = studentMapper.deleteById(id);
        if (delete == 0){
            return "删除失败";
        }
        return "删除成功";
    }

    //修改学生信息
    @ApiOperation("修改学生信息")
    @PutMapping("update")
    public String updateStudent(@RequestBody Student student){
        int update = studentMapper.updateById(student);
        if (update == 0){
            return "修改失败";
        }
        return "修改成功";
    }
    //根据id查询学生信息
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value = "学生id", required = true)
    })
    @ApiOperation("根据id查询学生信息")
    @GetMapping("user/{id}")
    public Student getStudent(@PathVariable Integer id){
        return studentMapper.selectById(id);
    }
    //获取所有学生信息
    @ApiOperation("获取所有学生信息")
    @GetMapping("all")
    public List<Student> getAllStudent(){
        return studentMapper.selectList(null);
    }
    //模糊查询---姓名
    @ApiOperation("模糊查询")
    @GetMapping("name")
    public List<Student> selectStudentsByName(@RequestParam String name) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return studentMapper.selectList(queryWrapper);
    }

    //模糊查询---地址
    @ApiOperation("模糊查询")
    @GetMapping("address")
    public List<Student> selectStudentByAddress(@RequestParam String address){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("address", address);
        return studentMapper.selectList(queryWrapper);
    }
}
